<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/8/20
 * Time: 下午3:49
 */
class PubServer
{
    private static $SERVER_ARR = array();
    private static $ID_ARR = array();

    public static function getSid($serverId){
        $modServer = new ModServer();
        $obj = $modServer->findByPk($serverId);
        return 'S'.$obj->server_num;
    }

    public static function getPkByDevId($devId){
        if(!isset(self::$ID_ARR[$devId])){
            $modServer = new ModServer();
            $obj = $modServer->findByAttributes(array(
                'develop_id' => $devId
            ));

            self::$ID_ARR[$devId] = $obj->id;
        }

        return self::$ID_ARR[$devId];
    }

    public static function getLogServerByDevId($devId){
        if(!isset(self::$SERVER_ARR[$devId])){
            $modServer = new ModServer();
            $obj = $modServer->findByAttributes(array(
                'develop_id' => $devId
            ));

            self::$SERVER_ARR[$devId] = $obj->log_server;
        }

        return self::$SERVER_ARR[$devId];
    }

    public static function getOptions($platform=null, $game=null, $key='develop_id'){

        if(empty($platform)){
            $platform = PubBase::getCurrentPlatform();
        }
        if(empty($game)){
            $game = PubBase::getCurrentGame();
        }

        $modServer = new ModServer();
        $result = $modServer->select(array(
            'p' => $platform,
            'g' => $game
        ));

        $options = array();
        foreach($result as $v){
            $options[$v[$key]] = $v['server_name'].' ('.$v['server_num'].')';
        }
        return $options;
    }

    public static function getOssApi($id){
        $modServer = new ModServer();
        $obj = $modServer->findByPk($id);
        return isset($obj->oss_api) ? $obj->oss_api : '';
    }

    public static function getServerByPk($id){
        if(empty($id)){
            return false;
        }
        $modServer = new ModServer();
        $obj = $modServer->findByPk($id);
        return $obj->getAttributes();
    }
    
    public static function getSDKServerByPk($id){
    	if(empty($id)){
    		return false;
    	}
    	$modSdkServer = new ModSdkServer();
    	$obj = $modSdkServer->findByPk($id);
    	return $obj->getAttributes();
    }
    
    /**
     * 获取渠道列表
     * @param string $game
     * @param string $key
     * @return multitype:unknown
     */
    public static function getPacketOptions($game=null, $key='id'){
    
    	if(empty($game)){
    		$game = PubBase::getCurrentGame();
    	}
    
    	$modChannel = new ModChannel();
    	$result = $modChannel->select(array(
    			'g' => $game
    	));
    
    	$options = array();
    	foreach($result as $v){
    		$options[$v[$key]] = $v['channel_name'];
    	}
    	return $options;
    }
    
    /**
     * 获取 服务器-》渠道 （一对多）的数据
     * @return Ambigous <multitype:, unknown>
     */
    public static function getServerChannel(){
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$result = $modServer->select(array(
    			'g' => $game
    	));
    	
    	$server_ids = '';
    	foreach($result as $key=>$v){
    		$server_ids.= ','.$v['id'];
    	}
    	$server_ids = substr($server_ids, 1);

    	$modServerChannel = new ModServerChannel();
    	$channel = $modServerChannel->selectChannel($server_ids,$game);
    	$channel_list = array();
    	
    	foreach ($channel as $key=>$val){
    		$channel_list[$val['server_id']][$key]['channel_id'] = $val['channel_id'];
    		$channel_list[$val['server_id']][$key]['channel_name'] = $val['channel_name'];
    	}

    	$options = array();
    	foreach ($result as $key=>$val){
    		$options[$key]['id'] = $val['id'];
    		$options[$key]['server_name'] = $val['server_name'];
    		$options[$key]['child'] = $channel_list[$val['id']];
    	}
    	
    	return $options;
    }
    
    /**
     * 获取 渠道-》服务器 （一对多）的数据
     * @return Ambigous <multitype:, unknown>
     */
    public static function getChannelServer(){
    	$game = PubBase::getCurrentGame();
    	$modChannel = new ModChannel();
    	$result = $modChannel->select(array(
    			'g' => $game
    	));
    	if(count($result) == 0) {
    		return $result;
    	}	 
    	$channel_ids = '';
    	foreach($result as $key=>$v){
    		$channel_ids.= ','.$v['id'];
    	}
    	$channel_ids = substr($channel_ids, 1);

    	$modServerChannel = new ModServerChannel();
    	$server = $modServerChannel->selectServel($channel_ids,$game);
    	$server_list = array();
    	 
    	foreach ($server as $key=>$val){
    		$server_list[$val['channel_id']][$key]['server_id'] = $val['server_id'];
    		$server_list[$val['channel_id']][$key]['server_name'] = $val['server_name'].' ('.$val['server_num'].')';
    	}
    
    	$options = array();
    	foreach ($result as $key=>$val){
    		$options[$val['tab']]['id'] = $val['id'];
    		$options[$val['tab']]['channel_name'] = $val['channel_name'];
    		$options[$val['tab']]['tab'] = $val['tab'];
    		$options[$val['tab']]['child'] = $server_list[$val['id']];
    	}
    	
    	// 刷选掉当前用户没有权限的渠道
    	$os_options = PubAuthSession::get('os');
    	$os_array = array();
    	foreach ($os_options as $key=>$val){
    		$os_array[] = $key;
    	}
    	
    	$list = array();
    	foreach ($options as $key=>$val){
    		if(in_array($key, $os_array)){
    			$list[] = $val;
    		}
    	}
    	
    	return $list;
    }

}